#include <iostream>
#include <vector>

#include "../luoo/protocol.hpp"

#include "../luoo/luoo_io_service_pool.hpp"
#include "client_session.hpp"

typedef std::vector<client_session*> client_pool;
int main (const int argc, const char* argv[])
{
	luoo::io_service_pool service_pool(6);
	service_pool.start();

	boost::asio::io_service ios;
	boost::asio::ip::tcp::resolver resolver(ios);
	boost::asio::ip::tcp::resolver::query query("192.168.0.106", "6666");
	boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);

	client_pool clients;
	for (unsigned int i = 0; i < 1; ++i)
	{
		clients.push_back(new client_session(service_pool.get_io_service()));
	}
	
	for (unsigned int i = 0; i < clients.size(); ++i)
	{
		clients[i]->start(iterator);
	}

	static int cnt = 0;
	while (true)
	{
		Login msg;
		msg.set_packet_count(1);
		luoo::uint16 len = msg.get_packet_length();
		const char* id = "595655989";
		memcpy(&msg.id, id, strlen(id) + 1);
		const char* pwd = "145286";
		memcpy(&msg.pwd, pwd, strlen(pwd) + 1);

		msg.set_packet_count(++cnt);
		msg.encode();

		for (unsigned int i = 0; i < clients.size(); ++i)
		{
			clients[i]->write(&msg, msg.get_packet_length());
			boost::this_thread::sleep(boost::posix_time::milliseconds(100));
		}
	}



	std::string input;
	while(true)
	{
		std::cin >> input;
		if (input == "exit")
		{
			break;
		}
	}

	service_pool.start();

	return 0;
}